package com.jacobson.huobi.components.toptab;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.jacobson.core.widget.logger.LoggerFactory;
import com.scwang.smartrefresh.layout.util.DensityUtil;

/**
 * @author WuChenZhong on 2019/7/20 22:54
 * @version 1.0.0
 */
public class TipView extends View {
    private static final int dp5 = DensityUtil.dp2px(5);
    private static final int dp4 = DensityUtil.dp2px(4);
    private static final int dp3 = DensityUtil.dp2px(3);
    private static final int dp2 = DensityUtil.dp2px(2);
    private Paint paint;
    private Rect rect = new Rect();
    private int number = 0;
    private int bottomNumber = 0;
    private boolean show = true;

    public TipView(Context context) {
        super(context);
        initialize();
    }

    public TipView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public TipView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    public void setNumber(int number, int bottomNumber) {
        this.number = number;
        this.bottomNumber = bottomNumber;
        setShow(number != 0 || bottomNumber != 0);
//        this.number = 5;
//        this.bottomNumber = bottomNumber;
//        setShow(true);
    }

    private void setShow(boolean show) {
        this.show = show;
        invalidate();
    }

    private void initialize() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setTextSize(DensityUtil.dp2px(10));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (show) {
            String text = String.valueOf(number);
            //返回包围整个字符串的最小的一个Rect区域
            paint.getTextBounds(text, 0, text.length(), rect);
            int max = Math.max(rect.width(), rect.height());
            int radius = (max >> 1) + dp3;
            int cx = getWidth() - radius - dp5;
            int cy = radius + dp3;
            int cyb = getHeight() - radius - dp3;
            if (number > 0) {
                paint.setColor(Color.TRANSPARENT);
                canvas.drawCircle(cx, cy, radius, paint);
                paint.setColor(Color.RED);
                canvas.drawText(text, 0, text.length(), cx, cy + getBaseline(paint), paint);
            }
//            LoggerFactory.INSTANCE.getLogger().d("tipView : " + cx + ", cy = " + cy + ", radius = " + radius);
            String bottomText = String.valueOf(bottomNumber);
            //返回包围整个字符串的最小的一个Rect区域
            if (bottomNumber > 0) {
                paint.getTextBounds(bottomText, 0, bottomText.length(), rect);
//            paint.setColor(Color.parseColor("#33A3DC"));
                paint.setColor(Color.TRANSPARENT);
                canvas.drawCircle(cx, cyb, radius, paint);
                paint.setColor(Color.BLACK);
                canvas.drawText(bottomText, 0, bottomText.length(), cx, cyb + getBaseline(paint), paint);
            }
        }
    }

    public static float getBaseline(Paint p) {
        Paint.FontMetrics fontMetrics = p.getFontMetrics();
        return (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
    }
}
